package com.anda.driver.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.content.Context;

import com.anda.driver.locator.R;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonCodeUtil {
	private static JsonCode jsonCode;
	private static JsonCodeUtil jsonCodeUtil = null;
	private Context context;
	private JsonCodeUtil(Context context){
		this.context = context;
		getObjectFromJson();
	}
	public static JsonCodeUtil getInstance(Context context){
		if ( jsonCodeUtil == null ) {
			jsonCodeUtil = new JsonCodeUtil(context);
		}
		return jsonCodeUtil;
	}
	
	public String getCodeText(String codeName, String codeValue){
		if (jsonCode.getStatus().equals("OK") && jsonCode.getData().containsKey(codeName)){
			if (jsonCode.getData().get(codeName).containsKey(codeValue)){
				return jsonCode.getData().get(codeName).get(codeValue);
			}
		}
		return "";
	}
	
	public LinkedHashMap<String, String> getMapCodeValues(String codeName){
		if (jsonCode.getStatus().equals("OK") && jsonCode.getData().containsKey(codeName)){
			return jsonCode.getData().get(codeName);
		} else {
			return null;
		}
	}
	
	public ArrayList<CodeValues> getListCodeValues(String codeName){
		ArrayList<CodeValues> list = new ArrayList<CodeValues>();
		if (jsonCode.getStatus().equals("OK") && jsonCode.getData().containsKey(codeName)){
			LinkedHashMap<String,String> map = jsonCode.getData().get(codeName);
			for(String k: map.keySet()){
				list.add(new CodeValues(k, map.get(k)));
			}
		} 
		return list;
	}
	
	private void getObjectFromJson(){
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			String json = CommUtil.readJsonData(context, R.raw.jsoncode);
			jsonCode = objectMapper.readValue(json, JsonCode.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static class CodeValues{
		String code;
		String value;
		public CodeValues(String code, String value) {
			this.code = code;
			this.value = value;
		}
		@Override
		public String toString() {
			return value;
		}
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
		
	}
	static class JsonCode {
		String status;
		LinkedHashMap<String, LinkedHashMap<String, String>> data;
		public String getStatus() {
			return status;
		}
		public void setStatus(String status) {
			this.status = status;
		}
		public LinkedHashMap<String, LinkedHashMap<String, String>> getData() {
			return data;
		}
		public void setData(LinkedHashMap<String, LinkedHashMap<String, String>> data) {
			this.data = data;
		}
		
	}
}
